package com.jevons.muffin.encrypt.rsa.net2android;

import android.util.Log;

import com.jevons.muffin.encrypt.Base64Coder;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;

/**
 * Rsa加解密（Android与.net端的交付）
 * <p/>
 * 【@link RSAUtils java和java之间的rsa加密】
 * 【@link http://blog.csdn.net/nairuohe/article/details/7180565】
 * 将.net提供的modulus 和 Exponent 转化成公钥
 * create at:  2016/5/20
 */
public class RsaHelper {

    private static final String TAG = RsaHelper.class.getSimpleName();

    /**
     * 生成RSA密钥对(默认密钥长度为1024)
     *
     * @return
     */
    public static KeyPair generateRSAKeyPair() {
        return generateRSAKeyPair(1024);
    }

    /**
     * 生成RSA密钥对
     *
     * @param keyLength 密钥长度，范围：512～2048
     * @return
     */
    public static KeyPair generateRSAKeyPair(int keyLength) {
        try {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA/ECB/PKCS1Padding");
            kpg.initialize(keyLength);
            return kpg.genKeyPair();
        } catch (NoSuchAlgorithmException e) {
            Log.w(TAG, e.toString());
            return null;
        }
    }

    /**
     * java端公钥转换成C#公钥
     *
     * @param key java端公钥
     * @return
     */
    public static String encodePublicKeyToXml(PublicKey key) {
        if (!RSAPublicKey.class.isInstance(key)) {
            return null;
        }
        RSAPublicKey pubKey = (RSAPublicKey) key;
        StringBuilder sb = new StringBuilder();

        sb.append("<RSAKeyValue>");
        sb.append("<Modulus>").append(
                Base64Coder.encode(pubKey.getModulus().toByteArray())).append(
                "</Modulus>");
        sb.append("<Exponent>").append(
                Base64Coder.encode(pubKey.getPublicExponent().toByteArray()))
                .append("</Exponent>");
        sb.append("</RSAKeyValue>");
        return sb.toString();
    }

    /**
     * C#端公钥转换成java公钥
     *
     * @param xml C#数据
     * @return
     */
    public static PublicKey decodePublicKeyFromXml(String xml) {
        xml = xml.replaceAll("\r", "").replaceAll("\n", "");
        BigInteger modulus = new BigInteger(1, Base64Coder.decode(
                getMiddleString(xml, "<Modulus>", "</Modulus>")));
        BigInteger publicExponent = new BigInteger(1, Base64Coder
                .decode(getMiddleString(xml, "<Exponent>",
                        "</Exponent>")));

        RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus,
                publicExponent);

        KeyFactory keyf;
        try {
            keyf = KeyFactory.getInstance("RSA");
            return keyf.generatePublic(rsaPubKey);
        } catch (Exception e) {
            Log.w(TAG, e.toString());
            return null;
        }
    }

    /**
     * C#端私钥转换成java私钥
     *
     * @param xml C#端私钥
     * @return
     */
    public static PrivateKey decodePrivateKeyFromXml(String xml) {
        xml = xml.replaceAll("\r", "").replaceAll("\n", "");
        BigInteger modulus = new BigInteger(1, Base64Coder.decode(
                getMiddleString(xml, "<Modulus>", "</Modulus>")));
        BigInteger publicExponent = new BigInteger(1, Base64Coder
                .decode(getMiddleString(xml, "<Exponent>",
                        "</Exponent>")));
        BigInteger privateExponent = new BigInteger(1, Base64Coder
                .decode(getMiddleString(xml, "<D>", "</D>")));
        BigInteger primeP = new BigInteger(1, Base64Coder.decode(
                getMiddleString(xml, "<P>", "</P>")));
        BigInteger primeQ = new BigInteger(1, Base64Coder.decode(
                getMiddleString(xml, "<Q>", "</Q>")));
        BigInteger primeExponentP = new BigInteger(1, Base64Coder
                .decode(getMiddleString(xml, "<DP>", "</DP>")));
        BigInteger primeExponentQ = new BigInteger(1, Base64Coder
                .decode(getMiddleString(xml, "<DQ>", "</DQ>")));
        BigInteger crtCoefficient = new BigInteger(1, Base64Coder
                .decode(getMiddleString(xml, "<InverseQ>",
                        "</InverseQ>")));

        RSAPrivateCrtKeySpec rsaPriKey = new RSAPrivateCrtKeySpec(modulus,
                publicExponent, privateExponent, primeP, primeQ,
                primeExponentP, primeExponentQ, crtCoefficient);

        KeyFactory keyf;
        try {
            keyf = KeyFactory.getInstance("RSA");
            return keyf.generatePrivate(rsaPriKey);
        } catch (Exception e) {
            Log.w(TAG, e.toString());
            return null;
        }
    }

    /**
     * java端私钥转换成C#私钥
     *
     * @param key java端私钥
     * @return
     */
    public static String encodePrivateKeyToXml(PrivateKey key) {
        if (!RSAPrivateCrtKey.class.isInstance(key)) {
            return null;
        }
        RSAPrivateCrtKey priKey = (RSAPrivateCrtKey) key;
        StringBuilder sb = new StringBuilder();

        sb.append("<RSAKeyValue>");
        sb.append("<Modulus>").append(
                Base64Coder.encode(priKey.getModulus().toByteArray())).append(
                "</Modulus>");
        sb.append("<Exponent>").append(
                Base64Coder.encode(priKey.getPublicExponent().toByteArray()))
                .append("</Exponent>");
        sb.append("<P>").append(
                Base64Coder.encode(priKey.getPrimeP().toByteArray())).append(
                "</P>");
        sb.append("<Q>").append(
                Base64Coder.encode(priKey.getPrimeQ().toByteArray())).append(
                "</Q>");
        sb.append("<DP>").append(
                Base64Coder.encode(priKey.getPrimeExponentP().toByteArray()))
                .append("</DP>");
        sb.append("<DQ>").append(
                Base64Coder.encode(priKey.getPrimeExponentQ().toByteArray()))
                .append("</DQ>");
        sb.append("<InverseQ>").append(
                Base64Coder.encode(priKey.getCrtCoefficient().toByteArray()))
                .append("</InverseQ>");
        sb.append("<D>").append(
                Base64Coder.encode(priKey.getPrivateExponent().toByteArray()))
                .append("</D>");
        sb.append("</RSAKeyValue>");
        return sb.toString();
    }

    /**
     * 用公钥加密
     *
     * @param data   需要加密的数据
     * @param pubKey 公钥
     * @return
     */
    public static byte[] encryptData(byte[] data, PublicKey pubKey) {
        try {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            return cipher.doFinal(data);
        } catch (Exception e) {
            Log.w(TAG, e.toString());
            return null;
        }
    }

    /**
     * 用私钥解密
     *
     * @param encryptedData 需要解密的数据
     * @param priKey        私钥
     * @return
     */
    public static byte[] decryptData(byte[] encryptedData, PrivateKey priKey) {
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, priKey);
            return cipher.doFinal(encryptedData);
        } catch (Exception e) {
            Log.w(TAG, e.toString());
            return null;
        }
    }

    /**
     * 根据指定公钥进行明文加密
     *
     * @param plainText 要加密的明文数据
     * @param pubKey    公钥
     * @return
     */
    public static String encryptDataFromStr(String plainText, PublicKey pubKey) {

        try {
            byte[] dataByteArray = plainText.getBytes("gb2312");
            byte[] encryptedDataByteArray = RsaHelper.encryptData(
                    dataByteArray, pubKey);
            return Base64Coder.encode(encryptedDataByteArray);
        } catch (UnsupportedEncodingException e) {
            Log.w(TAG, e.toString());
            return "";
        }
    }

    /**
     * 根据指定私钥对数据进行签名(默认签名算法为"SHA1withRSA")
     *
     * @param data   要签名的数据
     * @param priKey 私钥
     * @return
     */
    public static byte[] signData(byte[] data, PrivateKey priKey) {
        return signData(data, priKey, "SHA1withRSA");
    }

    /**
     * 根据指定私钥和算法对数据进行签名
     *
     * @param data      要签名的数据
     * @param priKey    私钥
     * @param algorithm 签名算法
     * @return
     */
    public static byte[] signData(byte[] data, PrivateKey priKey,
                                  String algorithm) {
        try {
            Signature signature = Signature.getInstance(algorithm);
            signature.initSign(priKey);
            signature.update(data);
            return signature.sign();
        } catch (Exception ex) {
            Log.w(TAG, ex.toString());
            return null;
        }
    }

    /**
     * 用指定的公钥进行签名验证(默认签名算法为"SHA1withRSA")
     *
     * @param data   数据
     * @param sign   签名结果
     * @param pubKey 公钥
     * @return
     */
    public static boolean verifySign(byte[] data, byte[] sign, PublicKey pubKey) {
        return verifySign(data, sign, pubKey, "SHA1withRSA");
    }

    /**
     * @param data      数据
     * @param sign      签名结果
     * @param pubKey    公钥
     * @param algorithm 签名算法
     * @return
     */
    public static boolean verifySign(byte[] data, byte[] sign,
                                     PublicKey pubKey, String algorithm) {
        try {
            Signature signature = Signature.getInstance(algorithm);
            signature.initVerify(pubKey);
            signature.update(data);
            return signature.verify(sign);
        } catch (Exception ex) {
            Log.w(TAG, ex.toString());
            return false;
        }
    }

    private static String getMiddleString(String str, String startStr, String endStr) {
        int beginIdx = str.indexOf(startStr) + startStr.length();
        int endIdx = str.indexOf(endStr);
        return str.substring(beginIdx, endIdx);
    }
}
